package co.gov.medellin.sdep.sisdep.model.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.List;

import javax.sql.DataSource;

import org.springframework.jdbc.core.SqlParameter;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
import org.springframework.jdbc.object.MappingSqlQuery;
import org.springframework.jdbc.object.SqlUpdate;

import co.gov.medellin.sdep.sisdep.model.FeriaDecFormVenObs;

public class FeriaDecFormVenObsDAOImpl extends JdbcDaoSupport implements IFeriaDecFormVenObsDAO {

    private InsertOperation insertOperation;

    private UpdateOperation updateOperation;

    private DeleteOperation deleteOperation;

    private DeleteByPrimaryKey deleteByPrimaryKey;

    private FindByPrimaryKeyQuery findByPrimaryKeyQuery;

    private FindByCriteriaQuery findByCriteriaQuery;

    private FindAllQuery findAllQuery;
    
    private FindByRadicadoQuery findByRadicadoQuery;

    private FindByActiveStateQuery findByActiveStateQuery;

	private static final String SELECT = "SELECT * FROM REG_FERIA_DEC_FORM_VEN_OBS_T ";
	private static final String WHERE = " WHERE ";
	private static final String PARAMETER = " ?, ";

    public void delete(FeriaDecFormVenObs feriaDecFormVenObs) {
		deleteOperation.execute( feriaDecFormVenObs  );
	}

	public void deleteByPrimaryKey(long id) {
		deleteByPrimaryKey.execute(id);
	}

	public FeriaDecFormVenObs findByPrimaryKey(long login) {
		return findByPrimaryKeyQuery.runQuery(login);
	}

	public void insert(FeriaDecFormVenObs feriaDecFormVenObs) {
		insertOperation.execute( feriaDecFormVenObs );
	}

	public void update(FeriaDecFormVenObs feriaDecFormVenObs) {
		updateOperation.execute( feriaDecFormVenObs );
	}

	public FeriaDecFormVenObs findByCriteria(FeriaDecFormVenObs  feriaDecFormVenObs) {
		return findByCriteriaQuery.runQuery(feriaDecFormVenObs);
	}

	public List load() {
		return findAllQuery.runQuery();
	}
	
	public List loadByRadicado(FeriaDecFormVenObs feriaDecFormVenObs) {
		return findByRadicadoQuery.runQuery(feriaDecFormVenObs);
	}

	public FeriaDecFormVenObs findByActiveState(FeriaDecFormVenObs  feriaDecFormVenObs) {
		return findByActiveStateQuery.runQuery(feriaDecFormVenObs);
	}

	public void initDao() {
    	insertOperation   = new InsertOperation( getDataSource() );
    	updateOperation = new UpdateOperation(  getDataSource() );
    	//deleteOperation = new DeleteOperation(  getDataSource() );
    	//deleteByPrimaryKey = new DeleteByPrimaryKey( getDataSource());
    	//findByPrimaryKeyQuery   = new FindByPrimaryKeyQuery(  getDataSource() );
    	//findByCriteriaQuery = new FindByCriteriaQuery(  getDataSource() );
    	findAllQuery = new FindAllQuery( getDataSource() );
    	findByRadicadoQuery = new FindByRadicadoQuery ( getDataSource() );
    	//findByActiveStateQuery = new FindByActiveStateQuery( getDataSource() );
    }

    // ------------- SaveOperation ---------------------------------------
	class InsertOperation extends SqlUpdate	{
		private static final String sql = "INSERT INTO REG_FERIA_DEC_FORM_VEN_OBS_T ("
				  + " ID, "
				  + " ID_FERIA_DEC_FORM_VENTA, "
				  + " CODIGO, "
				  + " ID_PROBLEMA, "				  
				  + " ID_CONTRAVENCION, "
				  + " ID_MOTIVO_DESCARTE, "
				  + " OBSERVACIONES, "
				  + " ESTADO, "
				  + " USUARIO_CREA, "
				  + " FECHA_CREA "				  
				  + ") VALUES ("
			  	  + " REG_FERIA_DEC_FORM_VEN_OBS_SEC.nextVal, "
				  + PARAMETER				  
				  + PARAMETER
				  + PARAMETER
				  + PARAMETER
				  + PARAMETER
				  + PARAMETER
				  + PARAMETER
				  + PARAMETER
				  + "SYSDATE"
				  + ")";
		public  InsertOperation(DataSource ds) {
			super(ds,sql);
			//set here required parameters			
			declareParameter( new SqlParameter(Types.INTEGER));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.INTEGER));
			declareParameter( new SqlParameter(Types.INTEGER));		
			declareParameter( new SqlParameter(Types.INTEGER));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));			
			compile();
		}
		public void execute(FeriaDecFormVenObs feriaDecFormVenObs) {
			InsertOperation.this.update( new Object[]{				
				feriaDecFormVenObs.getIdFeriaDecFormVenta(),
				feriaDecFormVenObs.getCodigo(),
				feriaDecFormVenObs.getIdProblema(),				
				feriaDecFormVenObs.getIdContravencion(),				
				feriaDecFormVenObs.getIdMotivoDescarte(),
				feriaDecFormVenObs.getObservaciones(),
				feriaDecFormVenObs.getEstado(),
				feriaDecFormVenObs.getUsuarioCrea()				
				});
		}
    }

    // ------------- UpdateOperation -------------------------------------
    class UpdateOperation extends SqlUpdate	{
    	private static final String sql = "UPDATE REG_FERIA_DEC_FORM_VEN_OBS_T SET "					  
					  + " ID_FERIA_DEC_FORM_VENTA = ? ,"
					  + " CODIGO = ? ,"
					  + " ID_PROBLEMA = ? ,"					  
					  + " ID_CONTRAVENCION = ? ,"					  
					  + " ID_MOTIVO_DESCARTE = ? ,"
					  + " OBSERVACIONES = ? ,"
					  + " ESTADO = ? ,"					  
					  + " USUARIO_MODIFICA = ? ,"
					  + " FECHA_MODIFICA = SYSDATE"
				  	  + WHERE
				  	  + " ID = ? ";
		public  UpdateOperation(DataSource ds) {
			super(ds,sql);
			//set here required parameters			
			declareParameter( new SqlParameter(Types.INTEGER));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.INTEGER));			
			declareParameter( new SqlParameter(Types.INTEGER));			
			declareParameter( new SqlParameter(Types.INTEGER));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.INTEGER));
			compile();
		}
		public void execute(FeriaDecFormVenObs feriaDecFormVenObs) {
			UpdateOperation.this.update( new Object[]{				
				feriaDecFormVenObs.getIdFeriaDecFormVenta(),
				feriaDecFormVenObs.getCodigo(),
				feriaDecFormVenObs.getIdProblema(),				
				feriaDecFormVenObs.getIdContravencion(),				
				feriaDecFormVenObs.getIdMotivoDescarte(),
				feriaDecFormVenObs.getObservaciones(),
				feriaDecFormVenObs.getEstado(),				
				feriaDecFormVenObs.getUsuarioModifica(),
				feriaDecFormVenObs.getId()
				});
		}
    }

    // ------------- DeleteOperation -------------------------------------
    class DeleteOperation extends SqlUpdate	{
		private static final String sql = "DELETE FROM REG_FERIA_DEC_FORM_VEN_OBS_T "
										+ WHERE
			  	  	  					+ " CODIGO = ? ";
		public DeleteOperation(DataSource ds) {
			super(ds,sql);
			declareParameter( new SqlParameter(Types.CHAR) );
			compile();
		}
		public void execute(FeriaDecFormVenObs feriaDecFormVenObs) {
			this.update( new Object[]{
				feriaDecFormVenObs.getCodigo(),
			});
		}
	}

    // ------------- DeleteByPrimaryKey ----------------------------------
    class DeleteByPrimaryKey extends SqlUpdate	{
		private static final String sql = "DELETE FROM REG_FERIA_DEC_FORM_VEN_OBS_T "
										+ WHERE + "1=1";
		public DeleteByPrimaryKey(DataSource ds) {
			super(ds,sql);
			compile();
		}	
		public void execute(long id) {
			this.update( new Object[]{
			});
		}	
    }

    // ---- BaseQueryOperation for Mapping ResultSet values --------------
	abstract class BaseQueryOperation extends MappingSqlQuery {
		public BaseQueryOperation(DataSource ds,String sql) {
	   		super(ds,sql);
		}	
		protected Object mapRow(ResultSet rs, int rownum) throws SQLException {
			FeriaDecFormVenObs feriaDecFormVenObs = new FeriaDecFormVenObs();
				feriaDecFormVenObs.setId(new Long(rs.getLong("ID")));
				feriaDecFormVenObs.setIdFeriaDecFormVenta(new Long(rs.getLong("ID_FERIA_DEC_FORM_VENTA")));
				feriaDecFormVenObs.setCodigo(rs.getString("CODIGO"));
				feriaDecFormVenObs.setIdProblema(new Long(rs.getLong("ID_PROBLEMA")));		
				feriaDecFormVenObs.setIdProblemaStr(rs.getString("ID_PROBLEMA"));		
				feriaDecFormVenObs.setIdContravencion(new Long(rs.getLong("ID_CONTRAVENCION")));
				feriaDecFormVenObs.setIdContravencionStr(rs.getString("ID_CONTRAVENCION"));
				feriaDecFormVenObs.setIdMotivoDescarte(new Long(rs.getLong("ID_MOTIVO_DESCARTE")));
				feriaDecFormVenObs.setIdMotivoDescarteStr(rs.getString("ID_MOTIVO_DESCARTE"));
				feriaDecFormVenObs.setObservaciones(rs.getString("OBSERVACIONES"));
				feriaDecFormVenObs.setEstado(rs.getString("ESTADO"));
				feriaDecFormVenObs.setUsuarioCrea(rs.getString("USUARIO_CREA"));
				feriaDecFormVenObs.setFechaCrea(rs.getDate("FECHA_CREA"));		
				feriaDecFormVenObs.setUsuarioModifica(rs.getString("USUARIO_MODIFICA"));
				feriaDecFormVenObs.setFechaModifica(rs.getDate("FECHA_MODIFICA"));
				feriaDecFormVenObs.setStrProblema(rs.getString("PROBLEMA"));
				feriaDecFormVenObs.setStrContravencion(rs.getString("CONTRAVENCION"));
				feriaDecFormVenObs.setStrMotivoDescarte(rs.getString("MOT_DESCARTE"));
			return feriaDecFormVenObs;
		}
    }

    // ------------- FindByPrimaryKeyQuery -------------------------------
    class FindByPrimaryKeyQuery extends BaseQueryOperation {
		private static final String sql = SELECT
										+ WHERE + "1=1";
    	public FindByPrimaryKeyQuery(DataSource ds) {
    		super(ds,sql);
			compile();
    	}
    	public FeriaDecFormVenObs runQuery(long id) {
			List result =  execute( new Object[]{new Long(id)});
			if ( result.size() > 0 ) {
        		return (FeriaDecFormVenObs) result.get(0);
        	} else {
        		return null;
        	}
    	}
    }

    // ------------- FindByCriteriaQuery ---------------------------------    
    class FindByCriteriaQuery extends BaseQueryOperation {
		private static final String sql = SELECT
										+ WHERE + " 1=1 ";
			//For required criteria elements uncomment required values
			  	  	  					//+ " AND ID = ? "
			  	  	  					//+ " AND ID_FERIA_DEC_FORM_VENTA = ? "
			  	  	  					//+ " AND CODIGO = ? "
			  	  	  					//+ " AND ID_PROBLEMA = ? "
			  	  	  					//+ " AND OBS_PROBLEMA = ? "
			  	  	  					//+ " AND ID_CONTRAVENCION = ? "
			  	  	  					//+ " AND OBS_CONTRAVENCION = ? "
			  	  	  					//+ " AND ID_MOTIVO_DESCARTE = ? "
			  	  	  					//+ " AND OBS_MOTIVO_DESCARTE = ? "
			  	  	  					//+ " AND ESTADO = ? "
			  	  	  					//+ " AND USUARIO_CREA = ? "
			  	  	  					//+ " AND FECHA_CREA = ? "
			  	  	  					//+ " AND USUARIO_MODIFICA = ? "
			  	  	  					//+ " AND FECHA_MODIFICA = ? "
										//+ "";
    	public FindByCriteriaQuery(DataSource ds) {
    		super(ds,sql);
    		//For required criteria elements uncomment required values
			//declareParameter( new SqlParameter(Types.INTEGER) );
			//declareParameter( new SqlParameter(Types.INTEGER) );
			//declareParameter( new SqlParameter(Types.CHAR) );
			//declareParameter( new SqlParameter(Types.INTEGER) );
			//declareParameter( new SqlParameter(Types.CHAR) );
			//declareParameter( new SqlParameter(Types.INTEGER) );
			//declareParameter( new SqlParameter(Types.CHAR) );
			//declareParameter( new SqlParameter(Types.INTEGER) );
			//declareParameter( new SqlParameter(Types.CHAR) );
			//declareParameter( new SqlParameter(Types.CHAR) );
			//declareParameter( new SqlParameter(Types.CHAR) );
			//declareParameter( new SqlParameter(Types.DATE) );
			//declareParameter( new SqlParameter(Types.CHAR) );
			//declareParameter( new SqlParameter(Types.DATE) );
			compile();
    	}
    	public FeriaDecFormVenObs runQuery(FeriaDecFormVenObs feriaDecFormVenObs) {
			List result =  execute( new Object[]{
			//For required criteria elements uncomment required values
				//feriaDecFormVenObs.getId(),
				//feriaDecFormVenObs.getIdFeriaDecFormVenta(),
				//feriaDecFormVenObs.getCodigo(),
				//feriaDecFormVenObs.getIdProblema(),
				//feriaDecFormVenObs.getObsProblema(),
				//feriaDecFormVenObs.getIdContravencion(),
				//feriaDecFormVenObs.getObsContravencion(),
				//feriaDecFormVenObs.getIdMotivoDescarte(),
				//feriaDecFormVenObs.getObsMotivoDescarte(),
				//feriaDecFormVenObs.getEstado(),
				//feriaDecFormVenObs.getUsuarioCrea(),
				//feriaDecFormVenObs.getFechaCrea(),
				//feriaDecFormVenObs.getUsuarioModifica(),
				//feriaDecFormVenObs.getFechaModifica(),
				});
			if ( result.size() > 0 ) {
        		return (FeriaDecFormVenObs) result.get(0);
        	} else {
        		return null;
        	}
    	}
    }

    // ------------- FindAllQuery ----------------------------------------    
    class FindAllQuery extends BaseQueryOperation {
		private static final String sql = SELECT
										+ WHERE + " 1=1 ";
    	public FindAllQuery(DataSource ds) {
    		super(ds,sql);
			compile();
    	}
    	public List runQuery() {
			List result =  execute();
			if ( result.size() > 0 ) {
        		return  result;
        	} else {
        		return null;
        	}
    	}
    }

    // ------------- FindAllQuery ----------------------------------------    
    class FindByRadicadoQuery extends BaseQueryOperation {
		private static final String sql = "SELECT OBS.*, MAE1.VRT_NOMBRE AS PROBLEMA,MAE2.VRT_NOMBRE AS CONTRAVENCION," 
										+ "MAE3.VRT_NOMBRE AS MOT_DESCARTE FROM REG_FERIA_DEC_FORM_VEN_OBS_T OBS INNER JOIN "  
										+ "REG_FERIA_DEC_FORM_VENTA_T VEN ON OBS.ID_FERIA_DEC_FORM_VENTA = VEN.ID INNER JOIN "
										+ "ADM_SOLICITUD_T SOL ON VEN.ID_SOLICITUD=SOL.ID INNER JOIN "
										+ "ADM_VALOR_TIPO_MAESTRA_T MAE1 ON OBS.ID_PROBLEMA =MAE1.ID_VALOR_TIPO INNER JOIN "
										+ "ADM_VALOR_TIPO_MAESTRA_T MAE2 ON OBS.ID_CONTRAVENCION =MAE2.ID_VALOR_TIPO INNER JOIN "
										+ "ADM_VALOR_TIPO_MAESTRA_T MAE3 ON obs.ID_MOTIVO_DESCARTE =MAE3.ID_VALOR_TIPO " 
										+ "WHERE SOL.RADICADO=?";
    	public FindByRadicadoQuery(DataSource ds) {
    		super(ds,sql);
    		declareParameter( new SqlParameter(Types.CHAR) );
			compile();
    	}
    	public List runQuery(FeriaDecFormVenObs feriaDecFormVenObs) {
			List result =  execute( new Object[]{
					feriaDecFormVenObs.getRadicado()
			});
			if ( result.size() > 0 ) {
        		return  result;
        	} else {
        		return null;
        	}
    	}
    }
    // ------------- FindByActiveStateQuery  -----------------------------
    class FindByActiveStateQuery extends BaseQueryOperation {
		private static final String sql = SELECT
										+ WHERE + "1=1";
    	public FindByActiveStateQuery(DataSource ds) {
    		super(ds,sql);
			compile();
    	}
    	public FeriaDecFormVenObs runQuery(FeriaDecFormVenObs feriaDecFormVenObs) {
			List result =  execute( new Object[]{
			});
			if ( result.size() > 0 ) {
        		return (FeriaDecFormVenObs) result.get(0);
        	} else {
        		return null;
        	}
    	}
    }
    

}
